/////////////////////////////////////////////////////////////////////////////
// Copyright(c) 2012-2015 purang All Rights Reserved
// Name:        message_kernel_impl.h
// Purpose:     
// Author:      syy
// Modified by: 
// Created:     2014/5/13 16:53
// RCS-ID:      
// Licence:     
/////////////////////////////////////////////////////////////////////////////
#ifndef _MESSAGE_KERNEL_IMPL_H_
#define _MESSAGE_KERNEL_IMPL_H_

#include "../include/message_kernel.h"
#include <core/observer/message_observer.h>

#pragma comment(lib,"winmm.lib")

namespace qb
{
    class ThreadWrapper;

	class CMessageKernel
		: public IMessageKernel  // Must be the first base class
        , public INetworkServiceObserver
        , public IMessageSyncObserver
	{
	public:
		CMessageKernel();
		virtual ~CMessageKernel();

		// IMessageKernel
		STDMETHOD_(int32_t,Create) (THIS_ INetworkService* ins, IMessageSync* sync, IMessageProcess* process);
		STDMETHOD_(int32_t,Destory) (THIS_);
        STDMETHOD_(int32_t,RegisterMessageKernelObserver) (THIS_ IMessageKernelObserver* observer);
        STDMETHOD_(int32_t,DeRegisterMessageKernelObserver) (THIS_ IMessageKernelObserver* observer);
        STDMETHOD_(int32_t,NotifyNetStatus) (THIS_ qb::NetStatus status, short retcode);
        STDMETHOD_(int32_t,RegisterNetwork) (THIS_ int32_t connect_id);
        STDMETHOD_(int32_t,DeRegisterNetwork) (THIS_ int32_t connect_id);
		STDMETHOD_(int32_t,CreateConnect) (THIS_);
		STDMETHOD_(int32_t,Connect) (THIS_ int32_t connect_id, const char* ip, uint32_t port);
		STDMETHOD_(int32_t,Send) (THIS_ int32_t connect_id, const char* buff, uint32_t bytes);
		STDMETHOD_(int32_t,CloseConnect) (THIS_ int32_t connect_id);

        // INetworkServiceObserver
        STDMETHOD_(bool,OnConnect) (THIS_ int32_t connect_id, int32_t error_id);
        STDMETHOD_(void,OnReceive) (THIS_ int32_t connect_id, MESSAGE_SHARED_PTR msp);
        STDMETHOD_(void,OnReceive2) (THIS_ int32_t connect_id, const char* buff, uint32_t bytes);
        STDMETHOD_(void,OnFailed) (THIS_ int32_t connect_id, int32_t error_id) {}

        // IMessageSyncObserver
        STDMETHOD_(void,OnNetData) (char* pRecvData, int nDataLen);
        STDMETHOD_(void,OnNetStatus) (int status) {}

    protected:
        // modules
        void AttachModules();
        void DetachModules();

    private:
		IMessageProcess*                    m_spMessageProcess;
		IMessageSync*			            m_spMessageSync;
	    INetworkService*                    m_spNetworkService;
        BList<IMessageKernelObserver*>		m_observers;
    };

}
#endif // _MESSAGE_KERNEL_IMPL_H_
